//
//  JCWecomeViewController.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/14.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit
import SDWebImage

class JCWecomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        //添加俩个控件 (头像,昵称)
        view.addSubview(headImageView)
        view.addSubview(welcomeLabel)
    }
    //view要显示的时候动画加载控件
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        headImageView.centerX = view.centerX
        headImageView.y = 200
        //显示头像
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.headImageView.y = 100
            }) { (finish) -> Void in
                //设置 "欢迎回来"
                self.welcomeLabel.centerX = self.headImageView.centerX
                self.welcomeLabel.y = CGRectGetMaxY(self.headImageView.frame) + 5
                self.welcomeLabel.alpha = 0
                //显示label
                self.welcomeLabel.hidden = false
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.welcomeLabel.alpha = 0.7
                    }, completion: { (finish) -> Void in
//                        //跳转控制器到首页
                     self.performSelector("toHome", withObject: self, afterDelay: 1)
                })
        }
    }
   @objc private func toHome(){
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    //切换根控制器    
        delegate.window?.rootViewController = JCTabBarController()
    }
    
    //头像的懒加载
   private lazy var headImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.size = CGSizeMake(90, 90)
        //设置圆角
        imageView.layer.cornerRadius = imageView.height * 0.5
        //图层覆盖
        imageView.layer.masksToBounds = true
        //设置边框
        imageView.layer.borderWidth = 2
        imageView.layer.borderColor = UIColor.darkGrayColor().CGColor
        
        //设置头像
        let urlString = JCUserAccount.loadUserAccount()!.avatar_large!
        let url = NSURL(string: urlString)
//        let url = NSURL(string: JCUserAccount.loadUserAccount()!.avatar_large!)!
        //加载图片(占位)
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        return imageView
    }()

    //昵称的懒加载
   private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        label.sizeToFit()
        //
        label.hidden = true
        
        return label
    }()
}
